import { ref } from "vue";
import { defineStore } from "pinia";

export const useFileStores = defineStore(
    "sy-fileInfo",
    () => {

        const select_tag = ref({
            gal: {
                tag: [],
                order: 0,
            },
            novel: {
                tag: [],
            }
        });
        //#region galtag检索
        const getSelectGalTag = () => {
            return select_tag.value.gal;
        }
        const setSelectGalTag = (data) => {
            select_tag.value.gal = data;
        }
        const getSelectGalTagToString = () => {
            const { tag, order } = select_tag.value.gal;
            const ret = {
                text: '',
                tagId: '',
                order: order,
            };
            if (tag && tag.length > 0) {
                const arr0 = [];
                const arr1 = [];
                for (let i = 0; i < tag.length; i++) {
                    const element = tag[i];
                    if (element.type == 0) {
                        arr0.push(element.name);
                    }
                    if (element.type == 1 || element.type == 2) {
                        arr1.push(element.id);
                    }
                }
                ret.text = arr0.toString();
                ret.tagId = arr1.toString();
            }
            return ret;
        }
        //#endregion

        //#region tag检索
        const getSelectTag = (type) => {
            return select_tag.value[type];
        }
        const setSelectTag = (type, data) => {
            select_tag.value[type] = data
        }
        //#endregion

        const getSelectNovelTagToString = () => {
            const { tag, order } = select_tag.value.novel;
            const ret = {
                text: '',
                tagId: '',
            };
            if (tag && tag.length > 0) {
                const arr0 = [];
                const arr1 = [];
                for (let i = 0; i < tag.length; i++) {
                    const element = tag[i];
                    if (element.type == 0) {
                        arr0.push(element.name);
                    }
                    if (element.type == 1 || element.type == 2) {
                        arr1.push(element.id);
                    }
                }
                ret.text = arr0.toString();
                ret.tagId = arr1.toString();
            }
            return ret;
        }




        return {
            select_tag,
            getSelectGalTag,
            setSelectGalTag,
            getSelectGalTagToString,
            getSelectNovelTagToString,

            getSelectTag,
            setSelectTag,
        };
    },
    {
        persist: true,
    }
);
